import EXIF from 'exif-js'
import JJLog from './JJLog';

export default class JJBase64Image {
    static formatPicture(params) {
        try{
            var fileInput = params.file; 
            var widthInput = params.width ? params.width : 750;
            var ratioInput = params.ratio ? params.ratio : 0.96;
            var callBack = params.callBack ? params.callBack : null;
            //var idParam = params.id;
            var file = fileInput.target.files[0];
            var reader = new FileReader();
            EXIF.getData(file, function () {
                //EXIF.getAllTags(this);
                orientation = EXIF.getTag(this, 'Orientation');
                reader.readAsDataURL(file);
            });
            //var maxImageSize = 300;
            /*判断图片方向*/
            var orientation = null;
            reader.onloadstart = function () {
            };
            reader.onerror = function () {
            };
            reader.onload = function (e) {
                var image = new Image();
                image.src = e.target.result;
                image.onload = function () {
                    var canvas = document.createElement("canvas");
                    var scale = image.width / image.height;
                    if (orientation === 6) {
                        //右转 90
                        canvas.width = image.height < widthInput ? image.height : widthInput;
                        canvas.height = parseInt(canvas.width * scale);
                        canvas.getContext("2d").rotate(90 * Math.PI / 180);
                        canvas.getContext("2d").drawImage(image, 0, 0, image.width, image.height, 0, -canvas.width, canvas.height, canvas.width);
                    } else if (orientation === 8) {
                        //左转 90
                        canvas.width = image.height < widthInput ? image.height : widthInput;
                        canvas.height = parseInt(canvas.width * scale);
                        canvas.getContext("2d").rotate(-90 * Math.PI / 180);
                        canvas.getContext("2d").drawImage(image, 0, 0, image.width, image.height, -canvas.height, 0, canvas.height, canvas.width);
                    } else if (orientation === 3) {
                        //右转 180
                        canvas.width = image.width < widthInput ? image.width : widthInput;
                        canvas.height = parseInt(canvas.width / scale);
                        canvas.getContext("2d").translate(canvas.width, 0);
                        canvas.getContext("2d").scale(-1, 1);
                        canvas.getContext("2d").translate(0, canvas.height);
                        canvas.getContext("2d").scale(1, -1);
                        canvas.getContext("2d").drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
                    } else {
                        //正常
                        canvas.width = image.width < widthInput ? image.width : widthInput;
                        canvas.height = parseInt(canvas.width / scale);
                        canvas.getContext("2d").drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
                    }
                    var imageUrl = canvas.toDataURL("image/jpeg", ratioInput)
                    var imageSize = JJBase64Image.getPicSize(imageUrl)
                    var maxImageSize = 300
                    if (imageSize > maxImageSize) {
                        var ratio = 0.92;
                        var lastImageSize = imageSize;
                        for (var i = 0; i < 23; i++) {
                            ratio = 0.92 - (i * 0.04);
                            imageUrl = canvas.toDataURL("image/jpeg", ratio);
                            imageSize = JJBase64Image.getPicSize(imageUrl);
    
                            if (imageSize === lastImageSize) {
                                break;
                            }
                            if (imageSize <= maxImageSize) {
                                break;
                            }
                            lastImageSize = imageSize;
                        }
                    }

                    JJLog.info('JJBase64Image: imageSize = ' + imageSize)

                    callBack&&callBack(imageUrl);
                }
            }
        }catch(e){
            JJLog.error(e)
        }
    }

    static getPicSize(base64url) {
        //获取base64图片大小，返回MB数字
        var str = base64url.replace('data:image/jpeg;base64,', '');
        var equalIndex = str.indexOf('=');
        if (equalIndex > 0) {
            str = str.substring(0, equalIndex);
        }
        var strLength = str.length;
        var fileLength = parseInt(strLength - (strLength / 8) * 2);
        // 由字节转换为MB
        var size = "";
        size = (fileLength / (1024 * 1)).toFixed(2);
        var sizeStr = size + "";                        //转成字符串
        // console.log(sizeStr);
        var index = sizeStr.indexOf(".");                    //获取小数点处的索引
        var dou = sizeStr.substr(index + 1, 2)            //获取小数点后两位的值
        if (dou === "00") {                                //判断后两位是否为00，如果是则删除00                
            return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
        }
        return parseInt(size);
    }
}
